package com.sens.orderback.ui.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.sens.bean.UserInfo;
import com.sens.common.StringUtil;
import com.sens.ipc.alipay.IPCHelper;
import com.sens.ipc.alipay.application.Callback;
import com.sens.ipc.alipay.hooker.Handle;
import com.sens.network.ApiByHttp;
import com.sens.network.ResponseCallback;
import com.sens.orderback.R;
import com.sens.utils.ToastUtil;
import com.sens.vo.AliPayOrderVo;
import com.yanzhenjie.nohttp.rest.Response;

public class LoginActivity extends Activity implements Callback {
    private ProgressDialog watingDialog;
    private AlertDialog.Builder hintDialog;
    private EditText userNameET;
    private EditText passwordET;
    private Handle handle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        userNameET = findViewById(R.id.userNameET);
        passwordET = findViewById(R.id.passwordET);
        initDialog();
        this.handle = IPCHelper.getHookerHandle(this, this);
    }

    private void initDialog() {
        watingDialog = new ProgressDialog(this);
        watingDialog.setIndeterminate(false);//循环滚动
        watingDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        watingDialog.setMessage("正在登录...");
        watingDialog.setCancelable(false);

        hintDialog = new AlertDialog.Builder(this);
        hintDialog.setCancelable(false);
        hintDialog.setTitle("提示");
    }

    public void login(View view) {
        String userName = userNameET.getText().toString().trim();
        if (StringUtil.isEmpty(userName)) {
            ToastUtil.showToast("账号不能为空");
            return;
        }
        String password = passwordET.getText().toString().trim();
        if (StringUtil.isEmpty(password)) {
            ToastUtil.showToast("密码不能为空");
            return;
        }
        watingDialog.show();
        ApiByHttp.getInstance().login(userName, password, loginCallback);
    }

    private ResponseCallback<UserInfo> loginCallback = new ResponseCallback<UserInfo>() {
        @Override
        public void onSucceed(int what, Response<UserInfo> response) {
            super.onSucceed(what, response);
            UserInfo userInfo = response.get();
            if (userInfo == null) {
                watingDialog.dismiss();
                ToastUtil.showToast("网络连接失败，请稍后重试");
                return;
            }
            if (userInfo.getStatus() == 0) {
                watingDialog.dismiss();
                ToastUtil.showToast(userInfo.getMsg());
                return;
            }
            startActivity(new Intent(LoginActivity.this, MainActivity.class));
            finish();
        }

        @Override
        public void onFailed(int what, Response<UserInfo> response) {
            super.onFailed(what, response);
            watingDialog.dismiss();
            ToastUtil.showToast("网络连接失败，请稍后重试");
        }
    };

    @Override
    public void noticeMessage(CharSequence message) {

    }

    @Override
    public void updateUserId(String userId) {

    }

    @Override
    public void requestMonitor() {

    }

    @Override
    public void addOrder(AliPayOrderVo aliPayOrderVo) {

    }
}
